<template>
  <div>
    <el-table
        :data="projectList"
        stripe
        border
        style="width: 100%; margin-top: 20px"
        v-loading="loading"
    >
      <el-table-column
          v-if="showIndex"
          type="index"
          label="序号"
          width="80"
          align="center"
          :index="indexMethod"
      />

      <!-- 动态列+进度条列 合并循环 -->
      <el-table-column
          v-for="col in columns"
          :key="col.prop || col.label"
          :prop="!col.isProgress ? col.prop : undefined"
          :label="col.label"
          :width="col.width"
          :min-width="col.minWidth || 120"
          :align="col.align || 'center'"
      >
        <!-- 如果是进度条列，使用插槽 -->
        <template v-if="col.isProgress" #default="scope">
          <el-progress
              :text-inside="true"
              :stroke-width="18"
              :percentage="scope.row.progress"
              :color="getProgressColor(scope.row.progress)"
              status="success"
          />
        </template>
        <!-- Switch列 -->
        <template v-else-if="col.isSwitch" #default="scope">
          <el-switch
              :model-value="scope.row[col.prop]"
              :active-value="col.activeValue ?? 1"
              :inactive-value="col.inactiveValue ?? 0"
              disabled
          />
        </template>

      </el-table-column>

      <!-- 操作列 -->
      <el-table-column fixed="right" label="操作" min-width="120" align="center"  v-if="isAdmin || hasOperateSlot">
        <template #default="scope">
<!--          <el-button-->
<!--              link-->
<!--              type="primary"-->
<!--              size="small"-->
<!--              @click="onDetail(scope.row)"-->
<!--          >-->
<!--            {{ detailButtonText }}-->
<!--          </el-button>-->
          <template v-if="isAdmin">
            <el-button
                link
                type="primary"
                size="small"
                @click="onUpdate(scope.row)"
            >
              编辑
            </el-button>
            <el-button
                link
                type="primary"
                size="small"
                @click="onRemove(scope.row)"
            >
              删除
            </el-button>
          </template>
       <template v-if="hasOperateSlot"> <!--扩展-->
         <slot name="operate" :row="scope.row"></slot>
       </template>

        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        background
        layout="sizes, prev, pager, next, total"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
        style="margin-top: 20px; text-align: right"
    />
  </div>
</template>

<script setup>

import { useAuthStore } from '@/stores/auth.js'

const authStore = useAuthStore()

const isAdmin = computed(() => {
  return authStore.token === 'admin'
})
// 检查是否传入了 operate 插槽
const slots = useSlots()
const hasOperateSlot = computed(() => !!slots.operate)

const props = defineProps({
  detailButtonText: {
    type: String,
    default: '详情'
  },

  projectList: {
    type: Array,
    required: true
  },
  columns: {
    type: Array,
    required: true
  },
  queryParams: {
    type: Object,
    required: true
  },
  total: {
    type: Number,
    required: true
  },
  loading: {
    type: Boolean,
    default: false
  },
  showIndex: {
    type: Boolean,
    default: true
  }
});

const emit = defineEmits(['update', 'remove', 'page-change', 'detail','page-change', 'page-size-change','switch-change']);

const indexMethod = (index) => {
  return index + 1;
};

const onDetail = (row) => {
  emit('detail', row)
}

const onUpdate = (row) => {
  emit('update', row);
};

const onRemove = (row) => {
  emit('remove', row);
};

const onPageChange = (page) => {
  emit('page-change', page);
};

const onPageSizeChange = (newSize) => {
  emit('page-size-change', newSize);
};

const getProgressColor = (percentage) => {
  if (percentage === 0) {
    return '#C0C4CC' // 灰色
  } else if (percentage > 0 && percentage < 100) {
    return '#409EFF' // 蓝色（Element 默认蓝）
  } else if (percentage === 100) {
    return '#67C23A' // 绿色（成功）
  }
}

// 新增 switch-change 事件
const onSwitchChange = (row, prop, value) => {
  emit('switch-change', { row, prop, value });
};

</script>
